package com.example.bbs.service;

import com.example.bbs.bean.Bbstag;
import com.example.bbs.bean.Bbstype;
import com.example.bbs.bean.BbstypeExample;
import com.example.bbs.dao.BbstagMapper;
import com.example.bbs.dao.BbstypeMapper;
import com.example.bbs.http.bean.type.HomePageData;
import com.example.bbs.http.bean.type.TagsSend;
import com.example.bbs.http.bean.type.TypeSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TypeService {

    @Autowired
    private BbstagMapper bbstagMapper;
    @Autowired
    private BbstypeMapper bbstypeMapper;

    public HomePageData homePageInit() {
        //分类列表
        List<Bbstag> bbstags = bbstagMapper.selectByExample(null);
        List<TagsSend> tagsSends = new ArrayList<>();
        for (Bbstag bbstag : bbstags) {
            TagsSend tagsSend = new TagsSend();
            tagsSend.setTagsId(bbstag.getTagid());
            tagsSend.setTagsName(bbstag.getTagname());
            BbstypeExample bbstypeExample = new BbstypeExample();
            bbstypeExample.createCriteria().andTagidEqualTo(bbstag.getTagid());
            //板块列表
            List<Bbstype> bbstypes = bbstypeMapper.selectByExample(bbstypeExample);
            List<TypeSend> typeSends = new ArrayList<>();
            for (Bbstype bbstype : bbstypes) {
                TypeSend typeSend = new TypeSend();
                typeSend.setTypeId(bbstype.getTypeid());
                typeSend.setTypeIcon(bbstype.getTypeicon());
                typeSend.setTypeName(bbstype.getTypename());
                typeSends.add(typeSend);
            }
            tagsSend.setTypeSends(typeSends);
            tagsSends.add(tagsSend);
        }
        HomePageData homePageData = new HomePageData();
        homePageData.setSuccess(1);
        homePageData.setMessage("ok");
        homePageData.setTags(tagsSends);
        return homePageData;
    }

}
